home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / jpeg-6a / jdmarker.c < prev    next >
C/C++ Source or Header  |  1996-01-13  |  31KB  |  1,056 lines

  1. /*
  2.  * jdmarker.c
  3.  *
  4.  * Copyright (C) 1991-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to decode JPEG datastream markers.
  9.  * Most of the complexity arises from our desire to support input
  10.  * suspension: if not all of the data for a marker is available,
  11.  * we must exit back to the application.  On resumption, we reprocess
  12.  * the marker.
  13.  */
  14.  
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18.  
  19.  
  20. typedef enum {            /* JPEG marker codes */
  21.   M_SOF0  = 0xc0,
  22.   M_SOF1  = 0xc1,
  23.   M_SOF2  = 0xc2,
  24.   M_SOF3  = 0xc3,
  25.   
  26.   M_SOF5  = 0xc5,
  27.   M_SOF6  = 0xc6,
  28.   M_SOF7  = 0xc7,
  29.   
  30.   M_JPG   = 0xc8,
  31.   M_SOF9  = 0xc9,
  32.   M_SOF10 = 0xca,
  33.   M_SOF11 = 0xcb,
  34.   
  35.   M_SOF13 = 0xcd,
  36.   M_SOF14 = 0xce,
  37.   M_SOF15 = 0xcf,
  38.   
  39.   M_DHT   = 0xc4,
  40.   
  41.   M_DAC   = 0xcc,
  42.   
  43.   M_RST0  = 0xd0,
  44.   M_RST1  = 0xd1,
  45.   M_RST2  = 0xd2,
  46.   M_RST3  = 0xd3,
  47.   M_RST4  = 0xd4,
  48.   M_RST5  = 0xd5,
  49.   M_RST6  = 0xd6,
  50.   M_RST7  = 0xd7,
  51.   
  52.   M_SOI   = 0xd8,
  53.   M_EOI   = 0xd9,
  54.   M_SOS   = 0xda,
  55.   M_DQT   = 0xdb,
  56.   M_DNL   = 0xdc,
  57.   M_DRI   = 0xdd,
  58.   M_DHP   = 0xde,
  59.   M_EXP   = 0xdf,
  60.   
  61.   M_APP0  = 0xe0,
  62.   M_APP1  = 0xe1,
  63.   M_APP2  = 0xe2,
  64.   M_APP3  = 0xe3,
  65.   M_APP4  = 0xe4,
  66.   M_APP5  = 0xe5,
  67.   M_APP6  = 0xe6,
  68.   M_APP7  = 0xe7,
  69.   M_APP8  = 0xe8,
  70.   M_APP9  = 0xe9,
  71.   M_APP10 = 0xea,
  72.   M_APP11 = 0xeb,
  73.   M_APP12 = 0xec,
  74.   M_APP13 = 0xed,
  75.   M_APP14 = 0xee,
  76.   M_APP15 = 0xef,
  77.   
  78.   M_JPG0  = 0xf0,
  79.   M_JPG13 = 0xfd,
  80.   M_COM   = 0xfe,
  81.   
  82.   M_TEM   = 0x01,
  83.   
  84.   M_ERROR = 0x100
  85. } JPEG_MARKER;
  86.  
  87.  
  88. /*
  89.  * Macros for fetching data from the data source module.
  90.  *
  91.  * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
  92.  * the current restart point; we update them only when we have reached a
  93.  * suitable place to restart if a suspension occurs.
  94.  */
  95.  
  96. /* Declare and initialize local copies of input pointer/count */
  97. #define INPUT_VARS(cinfo)  \
  98.     struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
  99.     const JOCTET * next_input_byte = datasrc->next_input_byte;  \
  100.     size_t bytes_in_buffer = datasrc->bytes_in_buffer
  101.  
  102. /* Unload the local copies --- do this only at a restart boundary */
  103. #define INPUT_SYNC(cinfo)  \
  104.     ( datasrc->next_input_byte = next_input_byte,  \
  105.       datasrc->bytes_in_buffer = bytes_in_buffer )
  106.  
  107. /* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */
  108. #define INPUT_RELOAD(cinfo)  \
  109.     ( next_input_byte = datasrc->next_input_byte,  \
  110.       bytes_in_buffer = datasrc->bytes_in_buffer )
  111.  
  112. /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
  113.  * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  114.  * but we must reload the local copies after a successful fill.
  115.  */
  116. #define MAKE_BYTE_AVAIL(cinfo,action)  \
  117.     if (bytes_in_buffer == 0) {  \
  118.       if (! (*datasrc->fill_input_buffer) (cinfo))  \
  119.         { action; }  \
  120.       INPUT_RELOAD(cinfo);  \
  121.     }  \
  122.     bytes_in_buffer--
  123.  
  124. /* Read a byte into variable V.
  125.  * If must suspend, take the specified action (typically "return FALSE").
  126.  */
  127. #define INPUT_BYTE(cinfo,V,action)  \
  128.     MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  129.           V = GETJOCTET(*next_input_byte++); )
  130.  
  131. /* As above, but read two bytes interpreted as an unsigned 16-bit integer.
  132.  * V should be declared unsigned int or perhaps INT32.
  133.  */
  134. #define INPUT_2BYTES(cinfo,V,action)  \
  135.     MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  136.           V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
  137.           MAKE_BYTE_AVAIL(cinfo,action); \
  138.           V += GETJOCTET(*next_input_byte++); )
  139.  
  140.  
  141. /*
  142.  * Routines to process JPEG markers.
  143.  *
  144.  * Entry condition: JPEG marker itself has been read and its code saved
  145.  *   in cinfo->unread_marker; input restart point is just after the marker.
  146.  *
  147.  * Exit: if return TRUE, have read and processed any parameters, and have
  148.  *   updated the restart point to point after the parameters.
  149.  *   If return FALSE, was forced to suspend before reaching end of
  150.  *   marker parameters; restart point has not been moved.  Same routine
  151.  *   will be called again after application supplies more input data.
  152.  *
  153.  * This approach to suspension assumes that all of a marker's parameters can
  154.  * fit into a single input bufferload.  This should hold for "normal"
  155.  * markers.  Some COM/APPn markers might have large parameter segments,
  156.  * but we use skip_input_data to get past those, and thereby put the problem
  157.  * on the source manager's shoulders.
  158.  *
  159.  * Note that we don't bother to avoid duplicate trace messages if a
  160.  * suspension occurs within marker parameters.  Other side effects
  161.  * require more care.
  162.  */
  163.  
  164.  
  165. LOCAL(boolean)
  166. get_soi (j_decompress_ptr cinfo)
  167. /* Process an SOI marker */
  168. {
  169.   int i;
  170.   
  171.   TRACEMS(cinfo, 1, JTRC_SOI);
  172.  
  173.   if (cinfo->marker->saw_SOI)
  174.     ERREXIT(cinfo, JERR_SOI_DUPLICATE);
  175.  
  176.   /* Reset all parameters that are defined to be reset by SOI */
  177.  
  178.   for (i = 0; i < NUM_ARITH_TBLS; i++) {
  179.     cinfo->arith_dc_L[i] = 0;
  180.     cinfo->arith_dc_U[i] = 1;
  181.     cinfo->arith_ac_K[i] = 5;
  182.   }
  183.   cinfo->restart_interval = 0;
  184.  
  185.   /* Set initial assumptions for colorspace etc */
  186.  
  187.   cinfo->jpeg_color_space = JCS_UNKNOWN;
  188.   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
  189.  
  190.   cinfo->saw_JFIF_marker = FALSE;
  191.   cinfo->density_unit = 0;    /* set default JFIF APP0 values */
  192.   cinfo->X_density = 1;
  193.   cinfo->Y_density = 1;
  194.   cinfo->saw_Adobe_marker = FALSE;
  195.   cinfo->Adobe_transform = 0;
  196.  
  197.   cinfo->marker->saw_SOI = TRUE;
  198.  
  199.   return TRUE;
  200. }
  201.  
  202.  
  203. LOCAL(boolean)
  204. get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
  205. /* Process a SOFn marker */
  206. {
  207.   INT32 length;
  208.   int c, ci;
  209.   jpeg_component_info * compptr;
  210.   INPUT_VARS(cinfo);
  211.  
  212.   cinfo->progressive_mode = is_prog;
  213.   cinfo->arith_code = is_arith;
  214.  
  215.   INPUT_2BYTES(cinfo, length, return FALSE);
  216.  
  217.   INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
  218.   INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
  219.   INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
  220.   INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
  221.  
  222.   length -= 8;
  223.  
  224.   TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
  225.        (int) cinfo->image_width, (int) cinfo->image_height,
  226.        cinfo->num_components);
  227.  
  228.   if (cinfo->marker->saw_SOF)
  229.     ERREXIT(cinfo, JERR_SOF_DUPLICATE);
  230.  
  231.   /* We don't support files in which the image height is initially specified */
  232.   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  233.   /* might as well have a general sanity check. */
  234.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  235.       || cinfo->num_components <= 0)
  236.     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  237.  
  238.   if (length != (cinfo->num_components * 3))
  239.     ERREXIT(cinfo, JERR_BAD_LENGTH);
  240.  
  241.   if (cinfo->comp_info == NULL)    /* do only once, even if suspend */
  242.     cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
  243.             ((j_common_ptr) cinfo, JPOOL_IMAGE,
  244.              cinfo->num_components * SIZEOF(jpeg_component_info));
  245.   
  246.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  247.        ci++, compptr++) {
  248.     compptr->component_index = ci;
  249.     INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
  250.     INPUT_BYTE(cinfo, c, return FALSE);
  251.     compptr->h_samp_factor = (c >> 4) & 15;
  252.     compptr->v_samp_factor = (c     ) & 15;
  253.     INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
  254.  
  255.     TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
  256.          compptr->component_id, compptr->h_samp_factor,
  257.          compptr->v_samp_factor, compptr->quant_tbl_no);
  258.   }
  259.  
  260.   cinfo->marker->saw_SOF = TRUE;
  261.  
  262.   INPUT_SYNC(cinfo);
  263.   return TRUE;
  264. }
  265.  
  266.  
  267. LOCAL(boolean)
  268. get_sos (j_decompress_ptr cinfo)
  269. /* Process a SOS marker */
  270. {
  271.   INT32 length;
  272.   int i, ci, n, c, cc;
  273.   jpeg_component_info * compptr;
  274.   INPUT_VARS(cinfo);
  275.  
  276.   if (! cinfo->marker->saw_SOF)
  277.     ERREXIT(cinfo, JERR_SOS_NO_SOF);
  278.  
  279.   INPUT_2BYTES(cinfo, length, return FALSE);
  280.  
  281.   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
  282.  
  283.   if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
  284.     ERREXIT(cinfo, JERR_BAD_LENGTH);
  285.  
  286.   TRACEMS1(cinfo, 1, JTRC_SOS, n);
  287.  
  288.   cinfo->comps_in_scan = n;
  289.  
  290.   /* Collect the component-spec parameters */
  291.  
  292.   for (i = 0; i < n; i++) {
  293.     INPUT_BYTE(cinfo, cc, return FALSE);
  294.     INPUT_BYTE(cinfo, c, return FALSE);
  295.     
  296.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  297.      ci++, compptr++) {
  298.       if (cc == compptr->component_id)
  299.     goto id_found;
  300.     }
  301.  
  302.     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
  303.  
  304.   id_found:
  305.  
  306.     cinfo->cur_comp_info[i] = compptr;
  307.     compptr->dc_tbl_no = (c >> 4) & 15;
  308.     compptr->ac_tbl_no = (c     ) & 15;
  309.     
  310.     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
  311.          compptr->dc_tbl_no, compptr->ac_tbl_no);
  312.   }
  313.  
  314.   /* Collect the additional scan parameters Ss, Se, Ah/Al. */
  315.   INPUT_BYTE(cinfo, c, return FALSE);
  316.   cinfo->Ss = c;
  317.   INPUT_BYTE(cinfo, c, return FALSE);
  318.   cinfo->Se = c;
  319.   INPUT_BYTE(cinfo, c, return FALSE);
  320.   cinfo->Ah = (c >> 4) & 15;
  321.   cinfo->Al = (c     ) & 15;
  322.  
  323.   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
  324.        cinfo->Ah, cinfo->Al);
  325.  
  326.   /* Prepare to scan data & restart markers */
  327.   cinfo->marker->next_restart_num = 0;
  328.  
  329.   /* Count another SOS marker */
  330.   cinfo->input_scan_number++;
  331.  
  332.   INPUT_SYNC(cinfo);
  333.   return TRUE;
  334. }
  335.  
  336.  
  337. METHODDEF(boolean)
  338. get_app0 (j_decompress_ptr cinfo)
  339. /* Process an APP0 marker */
  340. {
  341. #define JFIF_LEN 14
  342.   INT32 length;
  343.   UINT8 b[JFIF_LEN];
  344.   int buffp;
  345.   INPUT_VARS(cinfo);
  346.  
  347.   INPUT_2BYTES(cinfo, length, return FALSE);
  348.   length -= 2;
  349.  
  350.   /* See if a JFIF APP0 marker is present */
  351.  
  352.   if (length >= JFIF_LEN) {
  353.     for (buffp = 0; buffp < JFIF_LEN; buffp++)
  354.       INPUT_BYTE(cinfo, b[buffp], return FALSE);
  355.     length -= JFIF_LEN;
  356.  
  357.     if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
  358.       /* Found JFIF APP0 marker: check version */
  359.       /* Major version must be 1, anything else signals an incompatible change.
  360.        * We used to treat this as an error, but now it's a nonfatal warning,
  361.        * because some bozo at Hijaak couldn't read the spec.
  362.        * Minor version should be 0..2, but process anyway if newer.
  363.        */
  364.       if (b[5] != 1)
  365.     WARNMS2(cinfo, JWRN_JFIF_MAJOR, b[5], b[6]);
  366.       else if (b[6] > 2)
  367.     TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]);
  368.       /* Save info */
  369.       cinfo->saw_JFIF_marker = TRUE;
  370.       cinfo->density_unit = b[7];
  371.       cinfo->X_density = (b[8] << 8) + b[9];
  372.       cinfo->Y_density = (b[10] << 8) + b[11];
  373.       TRACEMS3(cinfo, 1, JTRC_JFIF,
  374.            cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  375.       if (b[12] | b[13])
  376.     TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);
  377.       if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
  378.     TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length);
  379.     } else {
  380.       /* Start of APP0 does not match "JFIF" */
  381.       TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN);
  382.     }
  383.   } else {
  384.     /* Too short to be JFIF marker */
  385.     TRACEMS1(cinfo, 1, JTRC_APP0, (int) length);
  386.   }
  387.  
  388.   INPUT_SYNC(cinfo);
  389.   if (length > 0)        /* skip any remaining data -- could be lots */
  390.     (*cinfo->src->skip_input_data) (cinfo, (long) length);
  391.  
  392.   return TRUE;
  393. }
  394.  
  395.  
  396. METHODDEF(boolean)
  397. get_app14 (j_decompress_ptr cinfo)
  398. /* Process an APP14 marker */
  399. {
  400. #define ADOBE_LEN 12
  401.   INT32 length;
  402.   UINT8 b[ADOBE_LEN];
  403.   int buffp;
  404.   unsigned int version, flags0, flags1, transform;
  405.   INPUT_VARS(cinfo);
  406.  
  407.   INPUT_2BYTES(cinfo, length, return FALSE);
  408.   length -= 2;
  409.  
  410.   /* See if an Adobe APP14 marker is present */
  411.  
  412.   if (length >= ADOBE_LEN) {
  413.     for (buffp = 0; buffp < ADOBE_LEN; buffp++)
  414.       INPUT_BYTE(cinfo, b[buffp], return FALSE);
  415.     length -= ADOBE_LEN;
  416.  
  417.     if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) {
  418.       /* Found Adobe APP14 marker */
  419.       version = (b[5] << 8) + b[6];
  420.       flags0 = (b[7] << 8) + b[8];
  421.       flags1 = (b[9] << 8) + b[10];
  422.       transform = b[11];
  423.       TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
  424.       cinfo->saw_Adobe_marker = TRUE;
  425.       cinfo->Adobe_transform = (UINT8) transform;
  426.     } else {
  427.       /* Start of APP14 does not match "Adobe" */
  428.       TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN);
  429.     }
  430.   } else {
  431.     /* Too short to be Adobe marker */
  432.     TRACEMS1(cinfo, 1, JTRC_APP14, (int) length);
  433.   }
  434.  
  435.   INPUT_SYNC(cinfo);
  436.   if (length > 0)        /* skip any remaining data -- could be lots */
  437.     (*cinfo->src->skip_input_data) (cinfo, (long) length);
  438.  
  439.   return TRUE;
  440. }
  441.  
  442.  
  443. LOCAL(boolean)
  444. get_dac (j_decompress_ptr cinfo)
  445. /* Process a DAC marker */
  446. {
  447.   INT32 length;
  448.   int index, val;
  449.   INPUT_VARS(cinfo);
  450.  
  451.   INPUT_2BYTES(cinfo, length, return FALSE);
  452.   length -= 2;
  453.   
  454.   while (length > 0) {
  455.     INPUT_BYTE(cinfo, index, return FALSE);
  456.     INPUT_BYTE(cinfo, val, return FALSE);
  457.  
  458.     length -= 2;
  459.  
  460.     TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
  461.  
  462.     if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  463.       ERREXIT1(cinfo, JERR_DAC_INDEX, index);
  464.  
  465.     if (index >= NUM_ARITH_TBLS) { /* define AC table */
  466.       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  467.     } else {            /* define DC table */
  468.       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  469.       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  470.       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  471.     ERREXIT1(cinfo, JERR_DAC_VALUE, val);
  472.     }
  473.   }
  474.  
  475.   INPUT_SYNC(cinfo);
  476.   return TRUE;
  477. }
  478.  
  479.  
  480. LOCAL(boolean)
  481. get_dht (j_decompress_ptr cinfo)
  482. /* Process a DHT marker */
  483. {
  484.   INT32 length;
  485.   UINT8 bits[17];
  486.   UINT8 huffval[256];
  487.   int i, index, count;
  488.   JHUFF_TBL **htblptr;
  489.   INPUT_VARS(cinfo);
  490.  
  491.   INPUT_2BYTES(cinfo, length, return FALSE);
  492.   length -= 2;
  493.   
  494.   while (length > 0) {
  495.     INPUT_BYTE(cinfo, index, return FALSE);
  496.  
  497.     TRACEMS1(cinfo, 1, JTRC_DHT, index);
  498.       
  499.     bits[0] = 0;
  500.     count = 0;
  501.     for (i = 1; i <= 16; i++) {
  502.       INPUT_BYTE(cinfo, bits[i], return FALSE);
  503.       count += bits[i];
  504.     }
  505.  
  506.     length -= 1 + 16;
  507.  
  508.     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  509.          bits[1], bits[2], bits[3], bits[4],
  510.          bits[5], bits[6], bits[7], bits[8]);
  511.     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  512.          bits[9], bits[10], bits[11], bits[12],
  513.          bits[13], bits[14], bits[15], bits[16]);
  514.  
  515.     if (count > 256 || ((INT32) count) > length)
  516.       ERREXIT(cinfo, JERR_DHT_COUNTS);
  517.  
  518.     for (i = 0; i < count; i++)
  519.       INPUT_BYTE(cinfo, huffval[i], return FALSE);
  520.  
  521.     length -= count;
  522.  
  523.     if (index & 0x10) {        /* AC table definition */
  524.       index -= 0x10;
  525.       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  526.     } else {            /* DC table definition */
  527.       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  528.     }
  529.  
  530.     if (index < 0 || index >= NUM_HUFF_TBLS)
  531.       ERREXIT1(cinfo, JERR_DHT_INDEX, index);
  532.  
  533.     if (*htblptr == NULL)
  534.       *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  535.   
  536.     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  537.     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  538.   }
  539.  
  540.   INPUT_SYNC(cinfo);
  541.   return TRUE;
  542. }
  543.  
  544.  
  545. LOCAL(boolean)
  546. get_dqt (j_decompress_ptr cinfo)
  547. /* Process a DQT marker */
  548. {
  549.   INT32 length;
  550.   int n, i, prec;
  551.   unsigned int tmp;
  552.   JQUANT_TBL *quant_ptr;
  553.   INPUT_VARS(cinfo);
  554.  
  555.   INPUT_2BYTES(cinfo, length, return FALSE);
  556.   length -= 2;
  557.  
  558.   while (length > 0) {
  559.     INPUT_BYTE(cinfo, n, return FALSE);
  560.     prec = n >> 4;
  561.     n &= 0x0F;
  562.  
  563.     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
  564.  
  565.     if (n >= NUM_QUANT_TBLS)
  566.       ERREXIT1(cinfo, JERR_DQT_INDEX, n);
  567.       
  568.     if (cinfo->quant_tbl_ptrs[n] == NULL)
  569.       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
  570.     quant_ptr = cinfo->quant_tbl_ptrs[n];
  571.  
  572.     for (i = 0; i < DCTSIZE2; i++) {
  573.       if (prec)
  574.     INPUT_2BYTES(cinfo, tmp, return FALSE);
  575.       else
  576.     INPUT_BYTE(cinfo, tmp, return FALSE);
  577.       /* We convert the zigzag-order table to natural array order. */
  578.       quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
  579.     }
  580.  
  581.     if (cinfo->err->trace_level >= 2) {
  582.       for (i = 0; i < DCTSIZE2; i += 8) {
  583.     TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
  584.          quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
  585.          quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
  586.          quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
  587.          quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
  588.       }
  589.     }
  590.  
  591.     length -= DCTSIZE2+1;
  592.     if (prec) length -= DCTSIZE2;
  593.   }
  594.  
  595.   INPUT_SYNC(cinfo);
  596.   return TRUE;
  597. }
  598.  
  599.  
  600. LOCAL(boolean)
  601. get_dri (j_decompress_ptr cinfo)
  602. /* Process a DRI marker */
  603. {
  604.   INT32 length;
  605.   unsigned int tmp;
  606.   INPUT_VARS(cinfo);
  607.  
  608.   INPUT_2BYTES(cinfo, length, return FALSE);
  609.   
  610.   if (length != 4)
  611.     ERREXIT(cinfo, JERR_BAD_LENGTH);
  612.  
  613.   INPUT_2BYTES(cinfo, tmp, return FALSE);
  614.  
  615.   TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
  616.  
  617.   cinfo->restart_interval = tmp;
  618.  
  619.   INPUT_SYNC(cinfo);
  620.   return TRUE;
  621. }
  622.  
  623.  
  624. METHODDEF(boolean)
  625. skip_variable (j_decompress_ptr cinfo)
  626. /* Skip over an unknown or uninteresting variable-length marker */
  627. {
  628.   INT32 length;
  629.   INPUT_VARS(cinfo);
  630.  
  631.   INPUT_2BYTES(cinfo, length, return FALSE);
  632.   
  633.   TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
  634.  
  635.   INPUT_SYNC(cinfo);        /* do before skip_input_data */
  636.   (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L);
  637.  
  638.   return TRUE;
  639. }
  640.  
  641.  
  642. /*
  643.  * Find the next JPEG marker, save it in cinfo->unread_marker.
  644.  * Returns FALSE if had to suspend before reaching a marker;
  645.  * in that case cinfo->unread_marker is unchanged.
  646.  *
  647.  * Note that the result might not be a valid marker code,
  648.  * but it will never be 0 or FF.
  649.  */
  650.  
  651. LOCAL(boolean)
  652. next_marker (j_decompress_ptr cinfo)
  653. {
  654.   int c;
  655.   INPUT_VARS(cinfo);
  656.  
  657.   for (;;) {
  658.     INPUT_BYTE(cinfo, c, return FALSE);
  659.     /* Skip any non-FF bytes.
  660.      * This may look a bit inefficient, but it will not occur in a valid file.
  661.      * We sync after each discarded byte so that a suspending data source
  662.      * can discard the byte from its buffer.
  663.      */
  664.     while (c != 0xFF) {
  665.       cinfo->marker->discarded_bytes++;
  666.       INPUT_SYNC(cinfo);
  667.       INPUT_BYTE(cinfo, c, return FALSE);
  668.     }
  669.     /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
  670.      * pad bytes, so don't count them in discarded_bytes.  We assume there
  671.      * will not be so many consecutive FF bytes as to overflow a suspending
  672.      * data source's input buffer.
  673.      */
  674.     do {
  675.       INPUT_BYTE(cinfo, c, return FALSE);
  676.     } while (c == 0xFF);
  677.     if (c != 0)
  678.       break;            /* found a valid marker, exit loop */
  679.     /* Reach here if we found a stuffed-zero data sequence (FF/00).
  680.      * Discard it and loop back to try again.
  681.      */
  682.     cinfo->marker->discarded_bytes += 2;
  683.     INPUT_SYNC(cinfo);
  684.   }
  685.  
  686.   if (cinfo->marker->discarded_bytes != 0) {
  687.     WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
  688.     cinfo->marker->discarded_bytes = 0;
  689.   }
  690.  
  691.   cinfo->unread_marker = c;
  692.  
  693.   INPUT_SYNC(cinfo);
  694.   return TRUE;
  695. }
  696.  
  697.  
  698. LOCAL(boolean)
  699. first_marker (j_decompress_ptr cinfo)
  700. /* Like next_marker, but used to obtain the initial SOI marker. */
  701. /* For this marker, we do not allow preceding garbage or fill; otherwise,
  702.  * we might well scan an entire input file before realizing it ain't JPEG.
  703.  * If an application wants to process non-JFIF files, it must seek to the
  704.  * SOI before calling the JPEG library.
  705.  */
  706. {
  707.   int c, c2;
  708.   INPUT_VARS(cinfo);
  709.  
  710.   INPUT_BYTE(cinfo, c, return FALSE);
  711.   INPUT_BYTE(cinfo, c2, return FALSE);
  712.   if (c != 0xFF || c2 != (int) M_SOI)
  713.     ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
  714.  
  715.   cinfo->unread_marker = c2;
  716.  
  717.   INPUT_SYNC(cinfo);
  718.   return TRUE;
  719. }
  720.  
  721.  
  722. /*
  723.  * Read markers until SOS or EOI.
  724.  *
  725.  * Returns same codes as are defined for jpeg_consume_input:
  726.  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  727.  */
  728.  
  729. METHODDEF(int)
  730. read_markers (j_decompress_ptr cinfo)
  731. {
  732.   /* Outer loop repeats once for each marker. */
  733.   for (;;) {
  734.     /* Collect the marker proper, unless we already did. */
  735.     /* NB: first_marker() enforces the requirement that SOI appear first. */
  736.     if (cinfo->unread_marker == 0) {
  737.       if (! cinfo->marker->saw_SOI) {
  738.     if (! first_marker(cinfo))
  739.       return JPEG_SUSPENDED;
  740.       } else {
  741.     if (! next_marker(cinfo))
  742.       return JPEG_SUSPENDED;
  743.       }
  744.     }
  745.     /* At this point cinfo->unread_marker contains the marker code and the
  746.      * input point is just past the marker proper, but before any parameters.
  747.      * A suspension will cause us to return with this state still true.
  748.      */
  749.     switch (cinfo->unread_marker) {
  750.     case M_SOI:
  751.       if (! get_soi(cinfo))
  752.     return JPEG_SUSPENDED;
  753.       break;
  754.  
  755.     case M_SOF0:        /* Baseline */
  756.     case M_SOF1:        /* Extended sequential, Huffman */
  757.       if (! get_sof(cinfo, FALSE, FALSE))
  758.     return JPEG_SUSPENDED;
  759.       break;
  760.  
  761.     case M_SOF2:        /* Progressive, Huffman */
  762.       if (! get_sof(cinfo, TRUE, FALSE))
  763.     return JPEG_SUSPENDED;
  764.       break;
  765.  
  766.     case M_SOF9:        /* Extended sequential, arithmetic */
  767.       if (! get_sof(cinfo, FALSE, TRUE))
  768.     return JPEG_SUSPENDED;
  769.       break;
  770.  
  771.     case M_SOF10:        /* Progressive, arithmetic */
  772.       if (! get_sof(cinfo, TRUE, TRUE))
  773.     return JPEG_SUSPENDED;
  774.       break;
  775.  
  776.     /* Currently unsupported SOFn types */
  777.     case M_SOF3:        /* Lossless, Huffman */
  778.     case M_SOF5:        /* Differential sequential, Huffman */
  779.     case M_SOF6:        /* Differential progressive, Huffman */
  780.     case M_SOF7:        /* Differential lossless, Huffman */
  781.     case M_JPG:            /* Reserved for JPEG extensions */
  782.     case M_SOF11:        /* Lossless, arithmetic */
  783.     case M_SOF13:        /* Differential sequential, arithmetic */
  784.     case M_SOF14:        /* Differential progressive, arithmetic */
  785.     case M_SOF15:        /* Differential lossless, arithmetic */
  786.       ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
  787.       break;
  788.  
  789.     case M_SOS:
  790.       if (! get_sos(cinfo))
  791.     return JPEG_SUSPENDED;
  792.       cinfo->unread_marker = 0;    /* processed the marker */
  793.       return JPEG_REACHED_SOS;
  794.     
  795.     case M_EOI:
  796.       TRACEMS(cinfo, 1, JTRC_EOI);
  797.       cinfo->unread_marker = 0;    /* processed the marker */
  798.       return JPEG_REACHED_EOI;
  799.       
  800.     case M_DAC:
  801.       if (! get_dac(cinfo))
  802.     return JPEG_SUSPENDED;
  803.       break;
  804.       
  805.     case M_DHT:
  806.       if (! get_dht(cinfo))
  807.     return JPEG_SUSPENDED;
  808.       break;
  809.       
  810.     case M_DQT:
  811.       if (! get_dqt(cinfo))
  812.     return JPEG_SUSPENDED;
  813.       break;
  814.       
  815.     case M_DRI:
  816.       if (! get_dri(cinfo))
  817.     return JPEG_SUSPENDED;
  818.       break;
  819.       
  820.     case M_APP0:
  821.     case M_APP1:
  822.     case M_APP2:
  823.     case M_APP3:
  824.     case M_APP4:
  825.     case M_APP5:
  826.     case M_APP6:
  827.     case M_APP7:
  828.     case M_APP8:
  829.     case M_APP9:
  830.     case M_APP10:
  831.     case M_APP11:
  832.     case M_APP12:
  833.     case M_APP13:
  834.     case M_APP14:
  835.     case M_APP15:
  836.       if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo))
  837.     return JPEG_SUSPENDED;
  838.       break;
  839.       
  840.     case M_COM:
  841.       if (! (*cinfo->marker->process_COM) (cinfo))
  842.     return JPEG_SUSPENDED;
  843.       break;
  844.  
  845.     case M_RST0:        /* these are all parameterless */
  846.     case M_RST1:
  847.     case M_RST2:
  848.     case M_RST3:
  849.     case M_RST4:
  850.     case M_RST5:
  851.     case M_RST6:
  852.     case M_RST7:
  853.     case M_TEM:
  854.       TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
  855.       break;
  856.  
  857.     case M_DNL:            /* Ignore DNL ... perhaps the wrong thing */
  858.       if (! skip_variable(cinfo))
  859.     return JPEG_SUSPENDED;
  860.       break;
  861.  
  862.     default:            /* must be DHP, EXP, JPGn, or RESn */
  863.       /* For now, we treat the reserved markers as fatal errors since they are
  864.        * likely to be used to signal incompatible JPEG Part 3 extensions.
  865.        * Once the JPEG 3 version-number marker is well defined, this code
  866.        * ought to change!
  867.        */
  868.       ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  869.       break;
  870.     }
  871.     /* Successfully processed marker, so reset state variable */
  872.     cinfo->unread_marker = 0;
  873.   } /* end loop */
  874. }
  875.  
  876.  
  877. /*
  878.  * Read a restart marker, which is expected to appear next in the datastream;
  879.  * if the marker is not there, take appropriate recovery action.
  880.  * Returns FALSE if suspension is required.
  881.  *
  882.  * This is called by the entropy decoder after it has read an appropriate
  883.  * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
  884.  * has already read a marker from the data source.  Under normal conditions
  885.  * cinfo->unread_marker will be reset to 0 before returning; if not reset,
  886.  * it holds a marker which the decoder will be unable to read past.
  887.  */
  888.  
  889. METHODDEF(boolean)
  890. read_restart_marker (j_decompress_ptr cinfo)
  891. {
  892.   /* Obtain a marker unless we already did. */
  893.   /* Note that next_marker will complain if it skips any data. */
  894.   if (cinfo->unread_marker == 0) {
  895.     if (! next_marker(cinfo))
  896.       return FALSE;
  897.   }
  898.  
  899.   if (cinfo->unread_marker ==
  900.       ((int) M_RST0 + cinfo->marker->next_restart_num)) {
  901.     /* Normal case --- swallow the marker and let entropy decoder continue */
  902.     TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
  903.     cinfo->unread_marker = 0;
  904.   } else {
  905.     /* Uh-oh, the restart markers have been messed up. */
  906.     /* Let the data source manager determine how to resync. */
  907.     if (! (*cinfo->src->resync_to_restart) (cinfo,
  908.                         cinfo->marker->next_restart_num))
  909.       return FALSE;
  910.   }
  911.  
  912.   /* Update next-restart state */
  913.   cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
  914.  
  915.   return TRUE;
  916. }
  917.  
  918.  
  919. /*
  920.  * This is the default resync_to_restart method for data source managers
  921.  * to use if they don't have any better approach.  Some data source managers
  922.  * may be able to back up, or may have additional knowledge about the data
  923.  * which permits a more intelligent recovery strategy; such managers would
  924.  * presumably supply their own resync method.
  925.  *
  926.  * read_restart_marker calls resync_to_restart if it finds a marker other than
  927.  * the restart marker it was expecting.  (This code is *not* used unless
  928.  * a nonzero restart interval has been declared.)  cinfo->unread_marker is
  929.  * the marker code actually found (might be anything, except 0 or FF).
  930.  * The desired restart marker number (0..7) is passed as a parameter.
  931.  * This routine is supposed to apply whatever error recovery strategy seems
  932.  * appropriate in order to position the input stream to the next data segment.
  933.  * Note that cinfo->unread_marker is treated as a marker appearing before
  934.  * the current data-source input point; usually it should be reset to zero
  935.  * before returning.
  936.  * Returns FALSE if suspension is required.
  937.  *
  938.  * This implementation is substantially constrained by wanting to treat the
  939.  * input as a data stream; this means we can't back up.  Therefore, we have
  940.  * only the following actions to work with:
  941.  *   1. Simply discard the marker and let the entropy decoder resume at next
  942.  *      byte of file.
  943.  *   2. Read forward until we find another marker, discarding intervening
  944.  *      data.  (In theory we could look ahead within the current bufferload,
  945.  *      without having to discard data if we don't find the desired marker.
  946.  *      This idea is not implemented here, in part because it makes behavior
  947.  *      dependent on buffer size and chance buffer-boundary positions.)
  948.  *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
  949.  *      This will cause the entropy decoder to process an empty data segment,
  950.  *      inserting dummy zeroes, and then we will reprocess the marker.
  951.  *
  952.  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  953.  * appropriate if the found marker is a future restart marker (indicating
  954.  * that we have missed the desired restart marker, probably because it got
  955.  * corrupted).
  956.  * We apply #2 or #3 if the found marker is a restart marker no more than
  957.  * two counts behind or ahead of the expected one.  We also apply #2 if the
  958.  * found marker is not a legal JPEG marker code (it's certainly bogus data).
  959.  * If the found marker is a restart marker more than 2 counts away, we do #1
  960.  * (too much risk that the marker is erroneous; with luck we will be able to
  961.  * resync at some future point).
  962.  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
  963.  * overrunning the end of a scan.  An implementation limited to single-scan
  964.  * files might find it better to apply #2 for markers other than EOI, since
  965.  * any other marker would have to be bogus data in that case.
  966.  */
  967.  
  968. GLOBAL(boolean)
  969. jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
  970. {
  971.   int marker = cinfo->unread_marker;
  972.   int action = 1;
  973.   
  974.   /* Always put up a warning. */
  975.   WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
  976.   
  977.   /* Outer loop handles repeated decision after scanning forward. */
  978.   for (;;) {
  979.     if (marker < (int) M_SOF0)
  980.       action = 2;        /* invalid marker */
  981.     else if (marker < (int) M_RST0 || marker > (int) M_RST7)
  982.       action = 3;        /* valid non-restart marker */
  983.     else {
  984.       if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
  985.       marker == ((int) M_RST0 + ((desired+2) & 7)))
  986.     action = 3;        /* one of the next two expected restarts */
  987.       else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
  988.            marker == ((int) M_RST0 + ((desired-2) & 7)))
  989.     action = 2;        /* a prior restart, so advance */
  990.       else
  991.     action = 1;        /* desired restart or too far away */
  992.     }
  993.     TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
  994.     switch (action) {
  995.     case 1:
  996.       /* Discard marker and let entropy decoder resume processing. */
  997.       cinfo->unread_marker = 0;
  998.       return TRUE;
  999.     case 2:
  1000.       /* Scan to the next marker, and repeat the decision loop. */
  1001.       if (! next_marker(cinfo))
  1002.     return FALSE;
  1003.       marker = cinfo->unread_marker;
  1004.       break;
  1005.     case 3:
  1006.       /* Return without advancing past this marker. */
  1007.       /* Entropy decoder will be forced to process an empty segment. */
  1008.       return TRUE;
  1009.     }
  1010.   } /* end loop */
  1011. }
  1012.  
  1013.  
  1014. /*
  1015.  * Reset marker processing state to begin a fresh datastream.
  1016.  */
  1017.  
  1018. METHODDEF(void)
  1019. reset_marker_reader (j_decompress_ptr cinfo)
  1020. {
  1021.   cinfo->comp_info = NULL;        /* until allocated by get_sof */
  1022.   cinfo->input_scan_number = 0;        /* no SOS seen yet */
  1023.   cinfo->unread_marker = 0;        /* no pending marker */
  1024.   cinfo->marker->saw_SOI = FALSE;    /* set internal state too */
  1025.   cinfo->marker->saw_SOF = FALSE;
  1026.   cinfo->marker->discarded_bytes = 0;
  1027. }
  1028.  
  1029.  
  1030. /*
  1031.  * Initialize the marker reader module.
  1032.  * This is called only once, when the decompression object is created.
  1033.  */
  1034.  
  1035. GLOBAL(void)
  1036. jinit_marker_reader (j_decompress_ptr cinfo)
  1037. {
  1038.   int i;
  1039.  
  1040.   /* Create subobject in permanent pool */
  1041.   cinfo->marker = (struct jpeg_marker_reader *)
  1042.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  1043.                 SIZEOF(struct jpeg_marker_reader));
  1044.   /* Initialize method pointers */
  1045.   cinfo->marker->reset_marker_reader = reset_marker_reader;
  1046.   cinfo->marker->read_markers = read_markers;
  1047.   cinfo->marker->read_restart_marker = read_restart_marker;
  1048.   cinfo->marker->process_COM = skip_variable;
  1049.   for (i = 0; i < 16; i++)
  1050.     cinfo->marker->process_APPn[i] = skip_variable;
  1051.   cinfo->marker->process_APPn[0] = get_app0;
  1052.   cinfo->marker->process_APPn[14] = get_app14;
  1053.   /* Reset marker processing state */
  1054.   reset_marker_reader(cinfo);
  1055. }
  1056.